/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.FuncionarioController;
import dao.LogradouroDAO;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

import model.FuncionarioMODEL;
import model.LogradouroMODEL;
import table.CellRenderer;
import table.funcionario.FuncionarioTable;
import table.TooltipCellRender;
import utilitarios.Cpf;
import utilitarios.Data;
import utilitarios.LimiteChar;
import utilitarios.Sexo;

/**
 *
 * @author Welisson
 */
public class Funcionarios extends javax.swing.JDialog {

    private static final int VISUALIZAR = 0;
    private static final int ADCIONAR = 1;
    private static final int EDITAR = 2;
    private static final int SELECIONAR = 3;

    private int status = VISUALIZAR;
    private int linhaSelecionada = -1;

    private List<FuncionarioMODEL> funcionarios;

    private JTable tblFuncionarios;
    private FuncionarioTable tableModel;
    private JScrollPane scrollPane;

    /**
     * Creates new form Teste
     */
    public Funcionarios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        setLocationRelativeTo(null);

        montaTabela();
        jTPesquisa.requestFocus();
        statusTela(status);
        limiteJTextFields();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTId = new javax.swing.JTextField();
        jTNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jCBSexo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTRg = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTEmailPrinc = new javax.swing.JTextField();
        jTEmailSec = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jTRua = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTCidade = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTBairro = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jTUF = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jTNum = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jFTNasc = new javax.swing.JFormattedTextField();
        jFTCpf = new javax.swing.JFormattedTextField();
        jFTTelCel = new javax.swing.JFormattedTextField();
        jFTTelRes = new javax.swing.JFormattedTextField();
        jFTTelCom = new javax.swing.JFormattedTextField();
        jFTCep = new javax.swing.JFormattedTextField();
        jPSenha = new javax.swing.JPasswordField();
        jPSenha2 = new javax.swing.JPasswordField();
        jLCpf = new javax.swing.JLabel();
        jLSenhas = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        jLTotFuncionarios = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jTPesquisa = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMNovo = new javax.swing.JMenu();
        jMSalvar = new javax.swing.JMenu();
        jMEditar = new javax.swing.JMenu();
        jMExcluir = new javax.swing.JMenu();
        jMLimpar = new javax.swing.JMenu();
        jMCancelar = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Funcionários");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(1200, 700));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel1.setText("Nome:");
        jLabel1.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel2.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel2.setText("Id:");
        jLabel2.setPreferredSize(new java.awt.Dimension(70, 10));

        jTId.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jTNome.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel3.setText("Sexo:");
        jLabel3.setPreferredSize(new java.awt.Dimension(70, 10));

        jCBSexo.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jCBSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino" }));
        jCBSexo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBSexoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel4.setText("RG:");
        jLabel4.setPreferredSize(new java.awt.Dimension(70, 10));

        jTRg.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel5.setText("CPF:");
        jLabel5.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel6.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel6.setText("Nascimento:");
        jLabel6.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel7.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel7.setText("Telefone Cel:");
        jLabel7.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel8.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel8.setText("Telefone Res:");
        jLabel8.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel9.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel9.setText("Tel Comercial:");
        jLabel9.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel10.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel10.setText("Email Princ:");
        jLabel10.setPreferredSize(new java.awt.Dimension(70, 10));

        jTEmailPrinc.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jTEmailSec.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel11.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel11.setText("Email Sec:");
        jLabel11.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel12.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel12.setText("CEP:");
        jLabel12.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel13.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel13.setText("Rua:");
        jLabel13.setPreferredSize(new java.awt.Dimension(70, 10));

        jTRua.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel14.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel14.setText("Cidade:");
        jLabel14.setPreferredSize(new java.awt.Dimension(70, 10));

        jTCidade.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel15.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel15.setText("Bairro:");
        jLabel15.setPreferredSize(new java.awt.Dimension(70, 10));

        jTBairro.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jTBairro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTBairroActionPerformed(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel17.setText("UF:");
        jLabel17.setPreferredSize(new java.awt.Dimension(70, 10));

        jTUF.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jTUF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTUFActionPerformed(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel18.setText("Numero:");
        jLabel18.setPreferredSize(new java.awt.Dimension(70, 10));

        jTNum.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jLabel16.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel16.setText("Senha:");
        jLabel16.setPreferredSize(new java.awt.Dimension(70, 10));

        jLabel19.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel19.setText("Conf Senha:");
        jLabel19.setPreferredSize(new java.awt.Dimension(70, 10));

        try {
            jFTNasc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTNasc.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        try {
            jFTCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTCpf.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jFTCpf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                validaCpf(evt);
            }
        });

        try {
            jFTTelCel.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)#####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTTelCel.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        try {
            jFTTelRes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTTelRes.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        try {
            jFTTelCom.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTTelCom.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        try {
            jFTCep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTCep.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jFTCep.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                preencheLogradouro(evt);
            }
        });

        jPSenha.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jPSenha2.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jPSenha2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ConfereSenhas(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTId, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTRg, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTEmailPrinc, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jFTTelCel, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jFTCep, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jTRua, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTEmailSec, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jFTCpf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jCBSexo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jFTTelRes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jFTTelCom, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jFTNasc, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jLCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jPSenha2, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLSenhas, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jTNum, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTUF, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFTCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jFTTelCom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jFTTelCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jFTTelRes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTEmailPrinc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTEmailSec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFTCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jFTNasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLSenhas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPSenha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 5, 1170, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1166, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 264, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 350, 1170, 270));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel21.setFont(new java.awt.Font("Bodoni Bk BT", 0, 14)); // NOI18N
        jLabel21.setText("TOTAL DE FUNCIONÁRIOS:");

        jLTotFuncionarios.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLTotFuncionarios.setText("0");

        jComboBox2.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CPF:", "ID:", "NOME:", "RG:", " " }));

        jTPesquisa.setFont(new java.awt.Font("Bodoni Bk BT", 0, 12)); // NOI18N

        jButton1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jButton1.setText("Pesquisar");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel21)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLTotFuncionarios, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 506, Short.MAX_VALUE)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                            .addComponent(jLTotFuncionarios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 290, 1170, -1));

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));
        jMenuBar1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jMenuBar1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N

        jMNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Add.png"))); // NOI18N
        jMNovo.setText("Novo");
        jMNovo.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMNovo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMNovoMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMNovo);

        jMSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Save.png"))); // NOI18N
        jMSalvar.setText("Salvar");
        jMSalvar.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMSalvarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMSalvar);

        jMEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Modify.png"))); // NOI18N
        jMEditar.setText("Editar");
        jMEditar.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMEditar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMEditarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMEditar);

        jMExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Close.png"))); // NOI18N
        jMExcluir.setText("Excluir");
        jMExcluir.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMExcluir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMExcluirMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMExcluir);

        jMLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Redo.png"))); // NOI18N
        jMLimpar.setText("Limpar");
        jMLimpar.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMLimpar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMLimparMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMLimpar);

        jMCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Back.png"))); // NOI18N
        jMCancelar.setText("Cancelar");
        jMCancelar.setFont(new java.awt.Font("Bodoni Bk BT", 0, 18)); // NOI18N
        jMCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMCancelarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMCancelar);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCBSexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBSexoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCBSexoActionPerformed

    private void jTBairroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTBairroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTBairroActionPerformed

    private void jTUFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTUFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTUFActionPerformed

    private void jMNovoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMNovoMouseClicked

        if (jMNovo.isEnabled()) {
            statusTela(ADCIONAR);
            jTNome.requestFocus();
        }
    }//GEN-LAST:event_jMNovoMouseClicked

    private void jMSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMSalvarMouseClicked
        if (jMSalvar.isEnabled()) {
            String nome = jTNome.getText();
            String sexo = (String) jCBSexo.getSelectedItem();
            String nasc = jFTNasc.getText();
            String dtNasc = jFTNasc.getText().replace("/", "");
            String rg = jTRg.getText();
            String cpf = jFTCpf.getText();
            String telCel = jFTTelCel.getText();
            String telRes = jFTTelRes.getText();
            String telCom = jFTTelCom.getText();
            String[] email = new String[2];
            email[0] = jTEmailPrinc.getText();
            email[1] = jTEmailSec.getText();
            String cep = jFTCep.getText().replace("-", "");
            String rua = jTRua.getText();
//            String cidade = jTCidade.getText();
//            String bairro = jTBairro.getText();
//            String uf = jTUF.getText();
            String num = jTNum.getText();

//            int funcao = funcoes.get(jCBFuncao.getSelectedIndex()).getId();
            String senha = new String(jPSenha.getPassword());
            String senha2 = new String(jPSenha2.getPassword());

            if (nome.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo NOME!");
                jTNome.requestFocus();
            } else if (sexo.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Selecione o SEXO!");
                jCBSexo.requestFocus();
            } else if (dtNasc.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo Data de Nascimento!");
                jFTNasc.requestFocus();
            } else if (!Data.dataValida(dtNasc)) {
                JOptionPane.showMessageDialog(this, "Data Inválida!");
                jFTNasc.requestFocus();
            } else if (rg.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo RG!");
                jTRg.requestFocus();
            } else if (cpf.trim().equals(".   .   -")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo CPF!");
                jFTCpf.requestFocus();
            } else if (telCel.replace("(", "").replace("-", "").replace(")", "").trim().equals("")
                    && telRes.replace("(", "").replace("-", "").replace(")", "").trim().equals("")
                    && telCom.replace("(", "").replace("-", "").replace(")", "").trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha pelo menos um Campo Telefone!");
                jFTTelCel.requestFocus();
            } else if (email[0].trim().equals("") && email[1].trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha pelo menos um Campo Email!");
                jTEmailPrinc.requestFocus();
            } else if (cep.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo CEP!");
                jFTCep.requestFocus();
            } else if (rua.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "CEP Inválido!");
                jFTCep.requestFocus();
            } else if (num.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha o campo NÚMERO!");
                jFTCep.requestFocus();
            } else if (senha.trim().equals("") && senha2.trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Preencha os campos de SENHA!");
                jPSenha.requestFocus();
            } else {

                List<String> emails = new ArrayList<String>();
                for (int i = 0; i < email.length; i++) {
                    if (!email[i].isEmpty()) {
                        emails.add(email[i]);
                    }
                }
                if (status == ADCIONAR) {
                    if (!senha.equals(senha2)) {
                        JOptionPane.showMessageDialog(this, "Senhas não Conferem!");
                        jPSenha2.requestFocus();
                    } else if (new FuncionarioController().existeCPF(cpf)) {
                        JOptionPane.showMessageDialog(this, "CPF já Existe!");
                        jFTCpf.requestFocus();
                    } else {
                        if (new FuncionarioController().salvar(
                                nome, cpf, rg, nasc, sexo, cep, senha,
                                emails, telCel, telRes, telCom, num, tableModel) != -1) {

                            JOptionPane.showMessageDialog(this, "Funcionario Cadastrado com Sucesso!");

                        } else {
                            JOptionPane.showMessageDialog(this, "Erro ao realizar cadastro!\nTente novamente mais tarde.");
                        }
                        statusTela(VISUALIZAR);
                    }
                } else if (status == EDITAR) {
                    String id = jTId.getText();

                    if (new FuncionarioController().alterar(id,
                            nome, cpf, rg, nasc, sexo, cep, senha,
                            emails, telCel, telRes, telCom, num, tableModel, linhaSelecionada)) {

                        JOptionPane.showMessageDialog(this, "Funcionario Editado com Sucesso!");
                    } else {
                        JOptionPane.showMessageDialog(this, "Erro ao Editar Funcionario!\nTente novamente mais tarde.");
                    }
                    statusTela(VISUALIZAR);
                }

            }

        }
    }//GEN-LAST:event_jMSalvarMouseClicked

    private void jMEditarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMEditarMouseClicked
        if (jMEditar.isEnabled()) {
            linhaSelecionada = tblFuncionarios.getSelectedRow();
            statusTela(EDITAR);
            FuncionarioMODEL func = funcionarios.get(linhaSelecionada);
            jTId.setText(String.valueOf(func.getId()));
            jTNome.setText(func.getNome());
            jCBSexo.setSelectedItem(Sexo.letraToSexo(func.getSexo()));
            jFTNasc.setText(Data.dateToString(func.getNasc()));
            jTRg.setText(func.getRg());
            jFTCpf.setText(func.getCpf());
            validaCpf(null);
            jFTTelCel.setText(func.getTelCel());
            jFTTelRes.setText(func.getTelRes());
            jFTTelCom.setText(func.getTelCom());
            //email
            List<String> email = func.getEmail();
            if (email.size() > 0) {
                jTEmailPrinc.setText(email.get(0));
            }
            if (email.size() > 1) {
                jTEmailSec.setText(email.get(1));
            }

            jFTCep.setText(String.valueOf(func.getCep()));
            preencheLogradouro(null);
            jTNum.setText(String.valueOf(func.getNum()));

            jPSenha.setText(func.getSenha());
            jPSenha2.setText(func.getSenha());

        }
    }//GEN-LAST:event_jMEditarMouseClicked

    private void jMExcluirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMExcluirMouseClicked
        if (jMExcluir.isEnabled()) {
            linhaSelecionada = tblFuncionarios.getSelectedRow();
            int excluir = JOptionPane.showConfirmDialog(this, "Deseja realmente Excluir esses Dados?", "Excluir", JOptionPane.YES_NO_OPTION);
            if (excluir == JOptionPane.YES_OPTION) {

                int id = funcionarios.get(linhaSelecionada).getId();
                if (new FuncionarioController().excluir(id, tableModel, linhaSelecionada)) {

                    JOptionPane.showMessageDialog(this, "Excluido com sucesso!");
                    statusTela(VISUALIZAR);
                } else {
                    JOptionPane.showMessageDialog(this, "Erro ao Excluir Funcionario!\n Tente Novamente mais tarde!");
                }
            }
//            else{
//                tblFuncionarios.getSelectionModel().clearSelection();
//            }

        }
    }//GEN-LAST:event_jMExcluirMouseClicked

    private void jMLimparMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMLimparMouseClicked
        if (jMLimpar.isEnabled()) {
            limpaCampos();
        }
    }//GEN-LAST:event_jMLimparMouseClicked

    private void jMCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMCancelarMouseClicked

        if (jMCancelar.isEnabled()) {
            limpaCampos();
            statusTela(VISUALIZAR);
        }
    }//GEN-LAST:event_jMCancelarMouseClicked

    private void preencheLogradouro(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_preencheLogradouro
        String cep = jFTCep.getText().replace("-", "").trim();
        if (cep.toCharArray().length == 8) {
            for (int i = 0; i < cep.toCharArray().length; i++) {

                if (String.valueOf(cep.charAt(i)).equals(" ")) {
                    return;
                }
            }

            LogradouroMODEL log = new FuncionarioController().getLougradouro(Integer.parseInt(cep));
            jTRua.setText(log.getEndereco());
            jTCidade.setText(log.getCidade());
            jTBairro.setText(log.getBairro());
            jTUF.setText(log.getUf());
            jTNum.requestFocus();

        }
    }//GEN-LAST:event_preencheLogradouro

    private void validaCpf(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_validaCpf
        String cpf = jFTCpf.getText();
        if (cpf.toCharArray().length == 14) {
            for (int i = 0; i < cpf.toCharArray().length; i++) {
                if (String.valueOf(cpf.charAt(i)).equals(" ")) {
                    return;
                }
            }
            Icon img;
            if (Cpf.verificaCpf(cpf)) {
                if (!new FuncionarioController().existeCPF(cpf)) {
                    img = new ImageIcon((getClass().getResource("/imagens/ok.fw.png")));
                    jLCpf.setIcon(img);
                } else {
                    img = new ImageIcon((getClass().getResource("/imagens/no.fw.png")));
                    jLCpf.setIcon(img);
                }
            } else {
                img = new ImageIcon((getClass().getResource("/imagens/no.fw.png")));
                jLCpf.setIcon(img);
            }
        }
    }//GEN-LAST:event_validaCpf

    private void ConfereSenhas(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ConfereSenhas
        String senha1 = new String(jPSenha.getPassword());
        String senha2 = new String(jPSenha2.getPassword());
        if (!"".equals(senha1) && !"".equals(senha2)) {
            Icon img;
            if (senha1.equals(senha2)) {
                img = new ImageIcon((getClass().getResource("/imagens/ok.fw.png")));
            } else {
                img = new ImageIcon((getClass().getResource("/imagens/no.fw.png")));
            }
            jLSenhas.setIcon(img);
        }
    }//GEN-LAST:event_ConfereSenhas

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCBSexo;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JFormattedTextField jFTCep;
    private javax.swing.JFormattedTextField jFTCpf;
    private javax.swing.JFormattedTextField jFTNasc;
    private javax.swing.JFormattedTextField jFTTelCel;
    private javax.swing.JFormattedTextField jFTTelCom;
    private javax.swing.JFormattedTextField jFTTelRes;
    private javax.swing.JLabel jLCpf;
    private javax.swing.JLabel jLSenhas;
    private javax.swing.JLabel jLTotFuncionarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMCancelar;
    private javax.swing.JMenu jMEditar;
    private javax.swing.JMenu jMExcluir;
    private javax.swing.JMenu jMLimpar;
    private javax.swing.JMenu jMNovo;
    private javax.swing.JMenu jMSalvar;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPasswordField jPSenha;
    private javax.swing.JPasswordField jPSenha2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTBairro;
    private javax.swing.JTextField jTCidade;
    private javax.swing.JTextField jTEmailPrinc;
    private javax.swing.JTextField jTEmailSec;
    private javax.swing.JTextField jTId;
    private javax.swing.JTextField jTNome;
    private javax.swing.JTextField jTNum;
    private javax.swing.JTextField jTPesquisa;
    private javax.swing.JTextField jTRg;
    private javax.swing.JTextField jTRua;
    private javax.swing.JTextField jTUF;
    // End of variables declaration//GEN-END:variables

    private void statusTela(int status) {
        if (status == VISUALIZAR) {
            this.status = VISUALIZAR;
            jMNovo.setEnabled(true);
            jMSalvar.setEnabled(false);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(false);
            jMLimpar.setEnabled(false);
            jMCancelar.setEnabled(false);
            tblFuncionarios.setEnabled(true);
            linhaSelecionada = -1;
            tblFuncionarios.getSelectionModel().clearSelection();
            jLTotFuncionarios.setText(String.valueOf(tblFuncionarios.getRowCount()));
            limpaCampos();
        } else if (status == ADCIONAR) {
            this.status = ADCIONAR;
            jMNovo.setEnabled(false);
            jMSalvar.setEnabled(true);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(false);
            jMLimpar.setEnabled(true);
            jMCancelar.setEnabled(true);
            tblFuncionarios.setEnabled(false);
        } else if (status == EDITAR) {
            this.status = EDITAR;
            jMNovo.setEnabled(false);
            jMSalvar.setEnabled(true);
            jMEditar.setEnabled(false);
            jMExcluir.setEnabled(true);
            jMLimpar.setEnabled(true);
            jMCancelar.setEnabled(true);
            tblFuncionarios.setEnabled(false);
        } else if (status == SELECIONAR) {
            this.status = SELECIONAR;
            jMNovo.setEnabled(true);
            jMSalvar.setEnabled(false);
            jMEditar.setEnabled(true);
            jMExcluir.setEnabled(true);
            jMLimpar.setEnabled(false);
            jMCancelar.setEnabled(false);

        }
        statusCampos(status);
    }

    private void statusCampos(int status) {
        if (status == VISUALIZAR) {

            jTId.setEditable(false);
            jTNome.setEditable(false);
            jCBSexo.setEnabled(false);
            jFTNasc.setEditable(false);
            jTRg.setEditable(false);
            jFTCpf.setEditable(false);
            jFTTelCel.setEditable(false);
            jFTTelRes.setEditable(false);
            jFTTelCom.setEditable(false);
            jTEmailPrinc.setEditable(false);
            jTEmailSec.setEditable(false);
            jFTCep.setEditable(false);
            jTRua.setEditable(false);
            jTCidade.setEditable(false);
            jTBairro.setEditable(false);
            jTUF.setEditable(false);
            jTNum.setEditable(false);

            jPSenha.setEditable(false);
            jPSenha2.setEditable(false);
        } else if (status == EDITAR) {

            jTId.setEditable(false);
            jTNome.setEditable(true);
            jCBSexo.setEnabled(true);
            jFTNasc.setEditable(true);
            jTRg.setEditable(true);
            jFTCpf.setEditable(false);
            jFTTelCel.setEditable(true);
            jFTTelRes.setEditable(true);
            jFTTelCom.setEditable(true);
            jTEmailPrinc.setEditable(true);
            jTEmailSec.setEditable(true);
            jFTCep.setEditable(true);
            jTRua.setEditable(false);
            jTCidade.setEditable(false);
            jTBairro.setEditable(false);
            jTUF.setEditable(false);
            jTNum.setEditable(true);

            jPSenha.setEditable(false);
            jPSenha2.setEditable(false);
        } else if (status == ADCIONAR) {

            jTId.setEditable(false);
            jTNome.setEditable(true);
            jCBSexo.setEnabled(true);
            jFTNasc.setEditable(true);
            jTRg.setEditable(true);
            jFTCpf.setEditable(true);
            jFTTelCel.setEditable(true);
            jFTTelRes.setEditable(true);
            jFTTelCom.setEditable(true);
            jTEmailPrinc.setEditable(true);
            jTEmailSec.setEditable(true);
            jFTCep.setEditable(true);
            jTRua.setEditable(false);
            jTCidade.setEditable(false);
            jTBairro.setEditable(false);
            jTUF.setEditable(false);
            jTNum.setEditable(true);

            jPSenha.setEditable(true);
            jPSenha2.setEditable(true);
        }
        jMNovo.requestFocus();
    }

    private void limpaCampos() {

        jTNome.setText("");
        jCBSexo.setSelectedIndex(0);
        jFTNasc.setText("");
        jTRg.setText("");
        if (status != EDITAR) {
            jTId.setText("");
            jFTCpf.setText("");
            jLCpf.setIcon(null);
            jPSenha.setText("");
            jPSenha2.setText("");
            jLSenhas.setIcon(null);
        }
        jFTTelCel.setText("");
        jFTTelRes.setText("");
        jFTTelCom.setText("");
        jTEmailPrinc.setText("");
        jTEmailSec.setText("");
        jFTCep.setText("");
        jTRua.setText("");
        jTCidade.setText("");
        jTBairro.setText("");
        jTUF.setText("");
        jTNum.setText("");

    }

    private void getListaFuncionarios() {

        funcionarios = new FuncionarioController().listar();

    }

    private List<LogradouroMODEL> getLogradouro() {
        List<LogradouroMODEL> log = new ArrayList<LogradouroMODEL>();
        for (FuncionarioMODEL func : funcionarios) {
            try {
                log.add(new LogradouroDAO().getLogradouro(func.getCep()));

            } catch (SQLException ex) {
                Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return log;
    }

    //METODOS DA TABELA
    private void montaTabela() {
        getListaFuncionarios();
        jPanel2.add(getTblFuncionarios());
        scrollPane.setBounds(5, 5, jPanel2.getWidth() - 10, jPanel2.getHeight() - 10);
    }

    private JScrollPane getTblFuncionarios() {
        if (tblFuncionarios == null) {
            tblFuncionarios = new JTable();
            // permite selecionar apenas uma linha de cada vez
            tblFuncionarios.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            tblFuncionarios.setDefaultRenderer(String.class, new CellRenderer());

            tblFuncionarios.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent me) {
                    //        System.out.println(linhaSelecionada);
                    if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
                        if (tblFuncionarios.isEnabled()) {

                            if (status == VISUALIZAR) {
                                statusTela(SELECIONAR);

                            }
                        }
                    } else if ((me.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) {
                        JOptionPane.showMessageDialog(null, "Isso não é algo Legal!");
                    } else if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
                        JPopupMenu menu = new JPopupMenu();
                        JMenuItem cliqueme = new JMenuItem("Copiar");
                        int col = tblFuncionarios.columnAtPoint(me.getPoint());
                        int row = tblFuncionarios.rowAtPoint(me.getPoint());
                        cliqueme.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent ae) {
                                copyClipboard(String.valueOf(tableModel.get(row, col)));
                            }
                        });

                        menu.add(cliqueme);

                        menu.show(tblFuncionarios, me.getX(), me.getY());
                    }
                }

            });
            tblFuncionarios.setModel(getTableModel());
            defineRenderers();
            scrollPane = new JScrollPane(tblFuncionarios);
        }
        return scrollPane;
    }

    private FuncionarioTable getTableModel() {
        if (tableModel == null) {
            tableModel = new FuncionarioTable(funcionarios, getLogradouro());

        }
        return tableModel;
    }

    private void defineRenderers() {
//        DefaultTableCellRenderer rendererCentro = new DefaultTableCellRenderer();
//        rendererCentro.setHorizontalAlignment(SwingConstants.CENTER);
//        DefaultTableCellRenderer rendererDireita = new DefaultTableCellRenderer();
//        rendererDireita.setHorizontalAlignment(SwingConstants.RIGHT);
//        DefaultTableCellRenderer rendererEsquerda = new DefaultTableCellRenderer();
//        rendererEsquerda.setHorizontalAlignment(SwingConstants.LEFT);

        JTableHeader header = tblFuncionarios.getTableHeader();
        header.setPreferredSize(new Dimension(0, 35));
        TableColumnModel modeloDaColuna = tblFuncionarios.getColumnModel();
        int[] tam = {35, 250, 100, 80, 70, 60, 100, 200, 65, 100, 60, 50, 25, 40};

        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            modeloDaColuna.getColumn(i).setMaxWidth(tam[i]);
            modeloDaColuna.getColumn(i).setCellRenderer(new TooltipCellRender());

        }

    }

    private void copyClipboard(String texto) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection selection = new StringSelection(texto);
        clipboard.setContents(selection, null);
    }

    private void limiteJTextFields() {
        jTRg.setDocument(new LimiteChar(12));
        jTNome.setDocument(new LimiteChar(50));
        jPSenha.setDocument(new LimiteChar(15));
        jPSenha2.setDocument(new LimiteChar(15));
        jTEmailPrinc.setDocument(new LimiteChar(50));
        jTEmailSec.setDocument(new LimiteChar(50));
    }

}
